//-----------------------------------------------------------------------
// <copyright file="a_products_specDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_products_specDAOBase
    {
        #region Common methods
        public virtual a_products_spec Createa_products_specFromReader(IDataReader reader)
        {
            a_products_spec item = new a_products_spec();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("Specsheetid"))) item.Specsheetid = (int)reader["Specsheetid"];
                if (!reader.IsDBNull(reader.GetOrdinal("Sheetcaption"))) item.Sheetcaption = (string)reader["Sheetcaption"];
                if (!reader.IsDBNull(reader.GetOrdinal("Specnamecaption"))) item.Specnamecaption = (string)reader["Specnamecaption"];
                if (!reader.IsDBNull(reader.GetOrdinal("Value1caption"))) item.Value1caption = (string)reader["Value1caption"];
                if (!reader.IsDBNull(reader.GetOrdinal("Value2caption"))) item.Value2caption = (string)reader["Value2caption"];
                if (!reader.IsDBNull(reader.GetOrdinal("Value3caption"))) item.Value3caption = (string)reader["Value3caption"];
                if (!reader.IsDBNull(reader.GetOrdinal("Spectype"))) item.Spectype = (bool)reader["Spectype"];
                if (!reader.IsDBNull(reader.GetOrdinal("Domainid"))) item.Domainid = (int)reader["Domainid"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_products_spec methods
            
        public virtual void Createa_products_spec(a_products_spec a_products_spec)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_products_specs_ins");
                
                database.AddInParameter(dbCommand, "@Sheetcaption", DbType.AnsiString, a_products_spec.Sheetcaption);
                database.AddInParameter(dbCommand, "@Specnamecaption", DbType.AnsiString, a_products_spec.Specnamecaption);
                database.AddInParameter(dbCommand, "@Value1caption", DbType.AnsiString, a_products_spec.Value1caption);
                database.AddInParameter(dbCommand, "@Value2caption", DbType.AnsiString, a_products_spec.Value2caption);
                database.AddInParameter(dbCommand, "@Value3caption", DbType.AnsiString, a_products_spec.Value3caption);
                database.AddInParameter(dbCommand, "@Spectype", DbType.Boolean, a_products_spec.Spectype);
                database.AddInParameter(dbCommand, "@Domainid", DbType.Int32, a_products_spec.Domainid);
                database.AddOutParameter(dbCommand, "@Specsheetid", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_products_spec.Specsheetid = (int)database.GetParameterValue(dbCommand, "@Specsheetid");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_products_spec methods
        
        public virtual void Updatea_products_spec(a_products_spec a_products_spec)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_products_specs_upd");            
                
                database.AddInParameter(dbCommand, "@Specsheetid", DbType.Int32, a_products_spec.Specsheetid);
                database.AddInParameter(dbCommand, "@Sheetcaption", DbType.AnsiString, a_products_spec.Sheetcaption);
                database.AddInParameter(dbCommand, "@Specnamecaption", DbType.AnsiString, a_products_spec.Specnamecaption);
                database.AddInParameter(dbCommand, "@Value1caption", DbType.AnsiString, a_products_spec.Value1caption);
                database.AddInParameter(dbCommand, "@Value2caption", DbType.AnsiString, a_products_spec.Value2caption);
                database.AddInParameter(dbCommand, "@Value3caption", DbType.AnsiString, a_products_spec.Value3caption);
                database.AddInParameter(dbCommand, "@Spectype", DbType.Boolean, a_products_spec.Spectype);
                database.AddInParameter(dbCommand, "@Domainid", DbType.Int32, a_products_spec.Domainid);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_products_spec methods
        public virtual void Deletea_products_spec(int specsheetid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_products_specs_del");
                
                database.AddInParameter(dbCommand, "@Specsheetid", DbType.Int32, specsheetid);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_products_spec methods
        
        public virtual a_products_spec Geta_products_spec(int specsheetid)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_products_specs_sel");
                
                database.AddInParameter(dbCommand, "@Specsheetid", DbType.Int32, specsheetid);
                
                a_products_spec a_products_spec =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_products_spec = Createa_products_specFromReader(reader);
                        reader.Close();
                    }
                }
                return a_products_spec;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_products_specList methods
        public virtual a_products_specCollection Geta_products_specList(a_products_specColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_products_specs_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_products_specCollection a_products_specCollection = new a_products_specCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_products_spec a_products_spec = Createa_products_specFromReader(reader);
                        a_products_specCollection.Add(a_products_spec);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_products_specCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_products_specCollection Geta_products_specList(a_products_specColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_products_specList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}